package Server.Poker.ZJH10;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sf.json.*;

import Server.Agent.GameAgent;
import Server.Model.Base.IRoom;
import Server.Model.Base.SHMUser;
import Server.Model.Hall.QpGameMgr;
import Server.Model.Hall.dthall;
import common.routine.GlobalSetting;
import Server.Model.Base.IServerUserItem;

public class ZjhTable10Mgr {
	private static class SingletonHolder {
		private static final ZjhTable10Mgr INSTANCE = new ZjhTable10Mgr();
	}
	public static final ZjhTable10Mgr getInstance() {
		return SingletonHolder.INSTANCE;
	}	
	public ZjhTable10Mgr() {

	}
	public static void OnRecv( GameAgent agent, JSONObject root){
		String data = root.optString("data");
		String vecArg2[] = data.split("\\|");
		if (vecArg2.length == 0){
			return;
		}
		String subcmd = vecArg2[0];
		switch (subcmd) {
		case "connect":
			Handle_onConnect(agent,vecArg2);
			return;
		case "join":
			Handle_onJoin(agent,vecArg2);
			return;
		case "leave":
			Handle_onLeave(agent,vecArg2);
			return;
		case "dismissroom":
			Handle_onDismissRoom(agent,vecArg2);
			return;			
		default:
			break;
		}
		
		Integer lUid = agent.getUid();
		ZjhTable10 tbl = (ZjhTable10)FindTable(lUid);
		if (tbl == null) {
			return;
		}
		switch (subcmd) {
		case "ready":
			tbl.Handle_onReady(lUid, vecArg2);
			break;
		case "robot":
			tbl.Handle_onRobot(lUid, vecArg2);
			break;
		case "biaoqing":
			tbl.Handle_onBiaoQing(lUid, vecArg2);
			break;
			
		default:
			break;
		}
		
		if (subcmd.equals("yuyin")) {
			tbl.Handle_onYuYin(lUid, vecArg2);
		}
		else if (subcmd.equals("chat")) {
			tbl.Handle_onChat(lUid, vecArg2);
		}
		else if (subcmd.equals("voice")) {
			tbl.Handle_onVoice(lUid, vecArg2);
		}
		else if (subcmd.equals("voicemsg")) {
			tbl.Handle_onVoiceMsg(lUid, vecArg2);
		}		
		else if (subcmd.equals("try_dismissroom")) {
			tbl.Handle_onTryDismissRoom(lUid, vecArg2);
		}
		else if (subcmd.equals("agree_dismissroom")) {
			tbl.Handle_onAgreeDismissRoom(lUid, vecArg2);
		}
		else if (subcmd.equals("refuse_dismissroom")) {
			tbl.Handle_onRefuseDismissRoom(lUid, vecArg2);
		}
		else if (subcmd.equals("start")) {
			tbl.Handle_onStart(lUid, vecArg2);
		}
		else	if (subcmd.equals("fangqi")) {
			tbl.Handle_onFangQi(lUid, vecArg2);
		}
		else	if (subcmd.equals("genzhu")) {
			tbl.Handle_onGenZhu(lUid, vecArg2);
		}
		else	if (subcmd.equals("jiazhu")) {
			tbl.Handle_onJiaZhu(lUid, vecArg2);
		}
		else	if (subcmd.equals("kanpai")) {
			tbl.Handle_onKanPai(lUid, vecArg2);
		}
		else	if (subcmd.equals("bipai")) {
			tbl.Handle_onBiPai(lUid, vecArg2);
		}
		else	if (subcmd.equals("kaipai")) {
			tbl.Handle_onKaiPai(lUid, vecArg2);
		}
		else	if (subcmd.equals("biaoqing")) {
			tbl.Handle_onBiaoQing(lUid, vecArg2);
		}
		else	if (subcmd.equals("yuyin")) {
			tbl.Handle_onYuYin(lUid, vecArg2);
		}else if (subcmd.equals("text")) {
			//tbl.Handle_onText(lUid, vecArg2);
		} 
		else if(subcmd.equals("expr")){
			tbl.Handle_onExpr(lUid, vecArg2);
		} 
		else	if (subcmd.equals("songli")) {
			tbl.Handle_onSongLi(lUid, vecArg2);
		}
		else	if (subcmd.equals("renewscore")) {
			tbl.Handle_onRenewScore(lUid, vecArg2);
		}
		else	if (subcmd.equals("transfergift")) {
			tbl.Handle_onUserTransferGift(lUid, vecArg2);
		}else if (subcmd.equals("lock")) {
			tbl.Handle_onLock(lUid, vecArg2);
		}else if (subcmd.equals("inBack")){
			tbl.Handle_InBack(lUid,vecArg2);
		} else if (subcmd.equals("inFore")){
			tbl.Handle_InFore(lUid,vecArg2);
		} else if (subcmd.equals("gameiap")){
			tbl.Handle_GameIap(lUid, vecArg2);
		} else if(subcmd.equals("quickMatch")){
			tbl.Handle_QuickMatch(lUid, vecArg2);
		}

		return;
	}
	public static IRoom  FindTable(Integer uid) {
		return QpGameMgr.getInstance().m_mapUserRoom.get(uid);
	}

	public void Handle_onHeart(GameAgent agent, String[] vecArg) {
		
	}

	public static void Handle_onConnect(GameAgent agent, String[] vecArg) {
		
		SUBCMD_S_Connect cmd = new SUBCMD_S_Connect();
		String sData = cmd.getJsonData();
		agent.SendMessage(sData);
	}

	public static void Handle_onJoin(GameAgent agent, String[] vecArg) {
		SUBCMD_S_Join cmd = new SUBCMD_S_Join();
		String sData = cmd.getJsonData();
		agent.SendMessage(sData);
	}

	public static void Handle_onLeave(GameAgent agent, String[] vecArg) {
		
		Integer _Uid = agent.getUid();
		ZjhTable10 pTable = (ZjhTable10)FindTable(_Uid);
		
		if (pTable != null)
		{
			pTable.OnUserStandUp(_Uid,true);
		}
		QpGameMgr.getInstance().removeUserRoom(_Uid);
		//m_mapHeartTime.remove(_Uid);
		
	}

	public static void Handle_onDismissRoom(GameAgent agent, String[] vecArg) {
		Integer _Uid = agent.getUid();
		ZjhTable10 tbl = (ZjhTable10)FindTable(_Uid);
		if(tbl != null)
		{
			QpGameMgr.getInstance().NotifyRemoveUserRoom(tbl);	
		}
		if(tbl != null && tbl.m_pIUserItem[0].dwUserID == _Uid && tbl.m_bTableStarted == false)
		{
			tbl.OnEventGameEnd(0, ZjhTable10.GAME_END_DISMISS);
		}
	}

	public void OnDeal(float dt) {
	}

	public static Random rd = new Random();

	public static int random(int min, int max, int f) {
		int r = rd.nextInt(max - min) + min;
		return r;
	}
/*
	static Map<Integer, ZjhTable> m_mapUID2Table = new HashMap<>();// 用户名 -> 房间
	static Map<Integer, ZjhTable> m_mapTable = new HashMap<>();// 房间号 -> 房间
*/
	public static ZjhTable10 CreateTable(int _Uid,String strParam,boolean isFangGuan) {

		try
		{
			int byCostGem = 1;
		
			SHMUser user = dthall.getInstance().getUserInfo(_Uid);
			if (user == null)	return null;
			
			if(strParam == null)	return null;
			
			String[] vecParam = strParam.split(";");
			
			if(vecParam.length == 0)	return null;
			String roomType = "ZUJU";
			
			int jushu = 0; String payway = "AAPAY"; 
			int fdFen = 0;
			int kouShuiJu = -1;
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];

				if (param.equalsIgnoreCase("8JU")) {
					jushu = 8;
					byCostGem = 15;
				}
				if (param.equalsIgnoreCase("16JU")) {
					jushu = 16;
					byCostGem = 30;
				}
				if (param.equalsIgnoreCase("24JU")) {
					jushu = 24;
					byCostGem = 45;
				}
				if(param.equalsIgnoreCase("88JU")){
					jushu = 88;//一定是个gold || dwc模式
				}

				if(param.equalsIgnoreCase("kou1")){
					kouShuiJu = 1;
				}

				if(param.equalsIgnoreCase("kou4")){
					kouShuiJu = 4;
				}
			}
			
			String strRoomDesc = "";
			for (int i = 0; i < vecParam.length; i++)
			{
				String param = vecParam[i];

				
				if (param.equalsIgnoreCase("AAPAY"))
				{
					 payway = "AAPAY";
					 strRoomDesc += "AA支付";
				}
				if (param.equalsIgnoreCase( "FZPAY"))
				{
					 payway = "FZPAY";
					 strRoomDesc += "房主支付";
				}
				if (param.equalsIgnoreCase( "WINPAY"))
				{
					 payway = "WINPAY";
					 strRoomDesc += "赢家支付";
				}
				
				if(param.equalsIgnoreCase("CLUBPAY")){
					 payway = "CLUBPAY";
					 strRoomDesc += "圈主支付";
				}	
				
				if(param.equalsIgnoreCase("DIZHU")){
					roomType = "GOLD";
				}
			}
			//必须放到DIZHU判断下面
			for(int i = 0;i < vecParam.length;i++){
				String param = vecParam[i];
				if (param.equalsIgnoreCase("DWC")) {
					roomType = "DWC";
					strRoomDesc = "真金";
					break;
				}
			}
			for (int i = 0; i < vecParam.length; i++)
			{			
				String param = vecParam[i];
				
				if (param.equalsIgnoreCase( "FD10"))
				{
					fdFen = 10;
					strRoomDesc += ",限注10";
				}
				if (param.equalsIgnoreCase("FD30"))
				{
					fdFen = 30;
					strRoomDesc += ",限注30";
				}
				if (param.equalsIgnoreCase("FD50"))
				{
					fdFen = 50;
					strRoomDesc += ",限注50";
				}
				if (param.equalsIgnoreCase("FD80"))
				{
					fdFen = 80;
					strRoomDesc += ",限注80";
				}		
				if (param.equalsIgnoreCase("FD100"))
				{
					fdFen = 100;
					strRoomDesc += ",限注100";
				}		
				if (param.equalsIgnoreCase("FD200"))
				{
					fdFen = 200;
					strRoomDesc += ",限注200";
				}		
				if (param.equalsIgnoreCase("FD500"))
				{
					fdFen = 500;
					strRoomDesc += ",限注500";
				}				
			}
			int diFen = 1;
			for (int i = 0; i < vecParam.length; i++)
			{			
				String param = vecParam[i];
				
				if (param.equalsIgnoreCase( "DI1"))
				{
					diFen = 1;
					strRoomDesc += ",底分1";
				}
				if (param.equalsIgnoreCase("DI3"))
				{
					diFen = 3;	
					strRoomDesc += ",底分3";						
				}
				if (param.equalsIgnoreCase("DI5"))
				{
					diFen = 5;
					strRoomDesc += ",底分5";
				}
				if (param.equalsIgnoreCase("DI10"))
				{
					diFen = 10;
					strRoomDesc += ",底分10";
				}
				if (param.equalsIgnoreCase("DI20"))
				{
					diFen = 20;
					strRoomDesc += ",底分20";
				}					
			}
			int menQuan = 0;
			for (int i = 0; i < vecParam.length; i++)
			{			
				String param = vecParam[i];
				
				if (param.equalsIgnoreCase( "MEN1"))
				{
					menQuan = 1;
				}
				if (param.equalsIgnoreCase("MEN3"))
				{
					menQuan = 3;						
				}
			}
			if(menQuan == 0){
				strRoomDesc += ",不闷";
			}else if(menQuan == 1){
				strRoomDesc += ",闷1圈";
			}else{
				strRoomDesc += ",闷3圈";
			}
			int maxCost = byCostGem;
			if(payway.equalsIgnoreCase("CLUBPAY")){
				maxCost = 0;
			}
			double dizhu = 0;
			double ruchang = 0;
			double lichang = 0;
			if(roomType.equalsIgnoreCase("GOLD")){
				maxCost = 0;
				byCostGem = 1;
				payway = "GOLD";
				dizhu = Double.parseDouble(vecParam[vecParam.length - 3]);
				ruchang = Double.parseDouble(vecParam[vecParam.length - 2]);
				lichang = Double.parseDouble(vecParam[vecParam.length - 1]);
			}else if(roomType.equalsIgnoreCase("DWC")){
				maxCost = 0;
				byCostGem = 1;
				payway = "DWC";
				dizhu = Double.parseDouble(vecParam[vecParam.length - 3]);
				ruchang = Double.parseDouble(vecParam[vecParam.length - 2]);
				lichang = Double.parseDouble(vecParam[vecParam.length - 1]);
			}
			System.out.println("1[" + maxCost +"]1");
			if (jushu > 0 && fdFen > 0 && byCostGem > 0 && payway.length() > 0 && user.getGem(0) >= maxCost) {				
				int factor = 0;
				for (int i = 0; i < 1000000; i++) {
					int rand_room = GlobalSetting.getInstance().getRandRoomNum();//random(100000, 999999, factor);
					factor = rand_room;
					ZjhTable10 room = (ZjhTable10) QpGameMgr.getInstance().getRoomById(rand_room);
					if (room == null) {
						room = new ZjhTable10();
						room.Init(ZjhTable10.INIT_TOTAL);
						room.SetParam(strParam);
						// room.setAppHandler(m_objAppHandler);
						room.m_dwTableID = rand_room;
						room.m_byCostGem = byCostGem;
						room.m_payWay = payway;
						room.m_lRoundLimit = (long) fdFen;
						room.m_byMaxRound = jushu;
						room.m_diFen = diFen;
						room.m_lCellScore = diFen;
						room.m_menQuan = menQuan;
						room.m_staticmenQuan = menQuan;
						room.m_Creator = _Uid;
						room.SetDesc(strRoomDesc);
						QpGameMgr.getInstance().putRoomIntoMap(rand_room, room);
						if(roomType.equalsIgnoreCase("GOLD")){
							//设置参数
							room.setBeiShu(dizhu, ruchang, lichang);
							room.setRoomType(roomType);
						}else if(roomType.equalsIgnoreCase("DWC")){
							//设置参数
							room.setBeiShu(dizhu, ruchang, lichang);
							room.setRoomType(roomType);
							if(kouShuiJu == 4){
								room.m_kouWinSumJu = 4;
							}else{
								room.m_kouWinSumJu = 0;
							}
						}			
						return room;
					}
				}
			}
		}catch(Exception e)
		{
			e.printStackTrace();
			ZjhTable10Mgr.getInstance().log.error(e.getMessage());
		}


		return null;
	}
	Log log = LogFactory.getLog( this .getClass());   
}
